package com.example.lcpractice.heading_offer;

import com.example.lcpractice.datastructure.TreeNode;

/**
 * [剑指 Offer 27]二叉树的镜像.java
 * 请完成一个函数，输入一个二叉树，该函数输出它的镜像。
 * <p>
 * 例如输入：
 * <p>
 * 4
 * /   \
 * 2     7
 * / \   / \
 * 1   3 6   9
 * 镜像输出：
 * 4
 * /   \
 * 7     2
 * / \   / \
 * 9   6 3   1
 * <p>
 * 示例 1：
 * 输入：root = [4,2,7,1,3,6,9]
 * 输出：[4,7,2,9,6,3,1]
 * 限制：
 * 0 <= 节点个数 <= 1000
 */
public class q27 {

    public static void main(String[] args) {
        TreeNode treeNode4 = new TreeNode(4);
        TreeNode treeNode7 = new TreeNode(7);
        TreeNode treeNode2 = new TreeNode(2);
        TreeNode treeNode9 = new TreeNode(9);
        TreeNode treeNode6 = new TreeNode(6);
        treeNode4.setLeft(treeNode7);
        treeNode4.setRight(treeNode2);
        treeNode7.setLeft(treeNode9);
        treeNode7.setRight(treeNode6);
        System.out.println(mirrorTree(treeNode4));
    }

    public static TreeNode mirrorTree(TreeNode root) {
        if (root == null) {
            return null;
        }
        TreeNode left = mirrorTree(root.getLeft());
        TreeNode right = mirrorTree(root.getRight());
        root.setLeft(right);
        root.setRight(left);
        return root;
    }

}
